package com.colorlight.cloud.complete.entity.instructions;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.complete.api.CompleteEntityScheduleActions;
import com.colorlight.cloud.complete.entity.enumerate.ExecutionType;
import com.colorlight.cloud.complete.entity.po.Download;
import com.colorlight.iot.proto.complete.instruction.CompleteInstruction;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 固化连接关系-file
 * 异步事件: SetConnectedRelationFinish
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class SetReceiverFile implements CompleteEntityScheduleActions {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class InputMsg implements Serializable {
        /**
         * 下载文件
         */
        private List<Download> download;
        /**
         * 是否发送
         */
        private ExecutionType isSend;
    }

    @Data
    @NoArgsConstructor
    @Builder
    public static class OutputMsg implements Serializable {
        // Output message does not contain any fields
    }

    private InputMsg inputMsg;
    private OutputMsg outputMsg;

    @Override
    public ByteString objectConvertBytes() {
        final CompleteInstruction.SetReceiverFile.InputMsg.Builder inputMsg =
                CompleteInstruction.SetReceiverFile.InputMsg.newBuilder();
        SetReceiverFile.InputMsg thisInput = this.getInputMsg();
        inputMsg.addAllDownload(thisInput.getDownload().stream().map(Download::toProto).collect(Collectors.toList()));
        inputMsg.setIsSend(thisInput.isSend.toProto());
        final CompleteInstruction.SetReceiverFile.Builder builder =
                CompleteInstruction.SetReceiverFile.newBuilder();
        builder.setInput(inputMsg);
        return builder.build().toByteString();
    }

    @Override
    public SetReceiverFile byteConvertObject(ByteString bytes) {
        this.setOutputMsg(new SetReceiverFile.OutputMsg());
        return this;
    }

}

